home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 011 / execnew.asm < prev    next >
Assembly Source File  |  1985-06-03  |  5KB  |  139 lines

  1. title    EXEC - Load & Execute Programs from Lattice 'C'
  2. page    64,132
  3. name    EXEC
  4. comment |    EXEC by Ted Reuss.  For free distribution only.   10/83
  5.  
  6. Corrected: SS is no set to PSP segment before call to EXEC.  4/9/84
  7.  
  8.    This code is not very elegant, but it seems to do what it was designed
  9. to do, so I am putting it the public domain.  When DOS loads a program
  10. either .com or .exe all available memory is assigned to it.  On the first
  11. call to EXEC memflg must be non-zero so that unused memory can be returned
  12. to DOS.  EXEC assumes that the stack segment is loaded higher in memory
  13. than all other segments.  This assumption holds true for many compilers.
  14.    Note that if you load & execute a program which in turn loads and executes
  15. another program via EXEC that memflg must be non-zero on the first call to
  16. EXEC by each child process.
  17.   A simple way to build the command line is:
  18.  
  19.      struct _line { char cnt; char cmd[250] } cmdline;
  20.      strcpy( cmdline.cmd, source );        /* move command string */
  21.      cmdline.cnt = strlen( cmdline.cmd );    /* set cnt */
  22.      cmdline.cmd[cmdline.cnt--] = '\r';         /* replace NULL with '\r'
  23.                            and decrement count */
  24.  
  25. Note that the NULL terminator is replaced by a Carriage Return and that
  26. the character count does not include either the CR or the count byte.
  27.  
  28. When using COMMAND.COM *file can be passed as NULL and the command line
  29. should begin with /C as per DOS 2.0 manual page F-1.
  30.  
  31. Be sure to pass a complete file specification with both drive and path name.
  32.  
  33. The File Control Blocks are not used in this version and I don't think DOS
  34. uses them anyway.
  35.  
  36. I added the parameter memflg today (4/3/84) for the public domain version.
  37. My runtime version of C.OBJ is modified to call the DOS function SETBLOCK (4AH)
  38. so that I do not need to adjust memory.  I tested the new version with DEBUG
  39. and everything seems to work ok.
  40.  
  41. Note: To open separate files as stdin or stdout for the child process:
  42.       1) close( N );  /* where N = 0 for stdin or 1 for stdout */
  43.       2) open( f,.... )  /* DOS always uses the lowest handle available.
  44.                 therefore if you closed stdin then next file
  45.                 opened will be assigned to stdin */
  46.  
  47.         Ted Reuss c/o South Texas Software, Inc.
  48.                   4544 Post Oak Place, Suite 222
  49.                   Houston, TX  77027
  50.                   713/877-8205
  51.     |
  52. PGROUP    GROUP    PROG
  53. PROG    SEGMENT BYTE PUBLIC 'PROG'
  54.     ASSUME    CS:PGROUP
  55. ;
  56. ; name        EXEC -- loads and executes a program
  57. ;
  58. ; synopsis    sta = exec( file, cmdlin, memflg );
  59. ;        short sta;    Return code
  60. ;                 = 0 if successful, else
  61. ;                 = DOS error number
  62. ;        char *file;    [d:][path]filename.ext
  63. ;                 if = NULL, COMMAND.COM is used.
  64. ;                      Drive/Path is hard coded.
  65. ;        char *cmdlin;    Ptr to command line of:
  66. ;                 1 byte -  number of characters in command
  67. ;                 bytes xx - command passed to program
  68. ;                 1 byte - carriage return
  69. ;        short memflg;    If != 0, then adjust memory allocation
  70. ;
  71.     PUBLIC    EXEC
  72. EXEC    PROC    NEAR
  73.     push    bp
  74.     mov    bp,sp
  75.     mov    cs:ss_save,ss        ;save ss
  76.     mov    cs:sp_save,sp        ;save sp
  77.     mov    bx,[bp+6]        ;ptr to command line
  78.     mov    word ptr cs:p_cmd,bx
  79.     mov    word ptr cs:p_cmd+2,ds
  80.     mov    si,[bp+4]        ;ptr to filename
  81.     test    si,si
  82.     jnz    exe10            ;if filename was passed
  83.     mov    si,offset cs:cmdcom    ;else use command.com
  84. exe10:
  85.     mov    ah,51h            ;get PSP segment
  86.     int    21h
  87.     mov    dx,bx            ; dx <- PSP segment
  88.     cmp    word ptr [bp+8],0    ;check memflg
  89.     jz    exe20
  90.     mov    es,bx            ; es <- PSP segment
  91.     mov    bx,ss
  92.     add    bx,1000h        ;add stack segment size
  93.     sub    bx,dx            ;subtract PSP
  94.     mov    ah,4ah            ;DOS setblock
  95.     int    21h
  96.     jc    exe30            ;if error
  97. exe20:
  98.     mov    bx,offset cs:parmblk
  99.     push    cs
  100.     pop    es            ; ES:BX -> parmblk
  101.     mov    dx,si            ; DS:DX -> file
  102.     mov    ax,4B00h        ;DOS exec function
  103.     int    21h
  104. exe30:
  105.     cli                ;interrupts off
  106.     mov    bx,cs:ss_save        ;restore ss
  107.     mov    ss,bx
  108.     mov    sp,cs:sp_save        ;restore sp
  109.     sti                ;interrupts back on
  110.     mov    ds,bx            ;restore ds
  111.     mov    es,bx            ;restore es
  112.     pop    bp            ;restore bp
  113.     jc    execx            ;if error, ax has error #
  114.     xor    ax,ax            ;clear return value
  115. execx:
  116.     ret                ;back to caller
  117.  
  118. ss_save dw    0            ;register save area
  119. sp_save dw    0
  120. ;
  121. parmblk equ    $            ;Parameter Block
  122. envir    dw    0            ;seg adr of environment
  123. p_cmd    dd    0            ;ptr to command line @ PSP+80h
  124. p_fcb1    dd    fcb            ;ptr to default FCB  @ PSP+5Ch
  125. p_fcb2    dd    fcb            ;ptr to default FCB  @ PSP+6Ch
  126.  
  127. fcb    db    0,11 dup(' '),4 dup(0)  ;default FCB
  128. cmdcom    db    'A:\COMMAND.COM',0
  129.  
  130. EXEC    ENDP
  131. PROG    ENDS
  132.     END
  133. 0,11 dup(' '),4 dup(0)  ;default FCB
  134. cmdcom    db    'A:\COMMAND.COM',0
  135.  
  136. EXEC    ENDP
  137. PROG    ENDS
  138.     END
  139.